package com.temi.iot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.temi.iot.common.constant.result.Result;
import com.temi.iot.entity.SysLogin;
import com.temi.iot.exception.BizExceptionEnum;
import com.temi.iot.exception.ThrowsKit;
import com.temi.iot.jwt.TokenInterface;
import com.temi.iot.jwt.model.TokenValue;
import com.temi.iot.mapper.SysLoginMapper;
import com.temi.iot.service.ISysLoginService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author one
 * @since 2019-12-11
 */
@Service
public class SysLoginServiceImpl implements ISysLoginService {

    @Override
    public Result login(String userName, String password) throws Exception {
        SysLogin sysLogin = SysLogin.builder().build().selectOne(new EntityWrapper().eq("user_name", userName)
                .eq("password", password));

        ThrowsKit.isEmpty(sysLogin,BizExceptionEnum.NO_THIS_USER);

        //查询相关权限

        String token =  TokenInterface.TOKEN.getToken(TokenValue.builder()
                .rid(sysLogin.getId())
                .per(new ArrayList<>())
                .build());
        return Result.success(token);
    }
}
